﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain;
using Repository;
using System.Data.SqlClient;
using System.Data;

namespace Controller
{
    public class ClienteController
    {

        private ClienteDB clienteDb = new ClienteDB(ConnectionFactory.getConnetcion());
        private CompraDB compraDb = new CompraDB(ConnectionFactory.getConnetcion());

        public void Connect(SqlConnection connection) 
        {
            try
            {
                clienteDb.putConnection(connection);
                compraDb.putConnection(connection);
            }
            catch (SqlException sqlex) 
            {
            }
        }

        public Cliente login(string usuario, string senha) 
        {
            Cliente c = new Cliente();
            c.Usuario = usuario;
            c.Senha = senha;
            Cliente client = new Cliente();
            clienteDb.openConnection();
            client = clienteDb.Login(c);
            clienteDb.closeConnection();
            return client;
            
        }

        public DataTable PreencheDataGridViewClientes()
        {
            DataTable dt = new DataTable();
            clienteDb.openConnection();
            dt =  clienteDb.AuxDataGridViewClientes();
            clienteDb.closeConnection();
            return dt;
        }


        public bool cadastrarCliente(Cliente cliente)
        {
            Boolean result;
            try
            {
                
                IQuery<Cliente> consult = clienteDb;
                clienteDb.openConnection();
                result = consult.save(cliente);
                clienteDb.closeConnection();
                return result;
            }
            catch (Exception ex) 
            {
                return false;
            }
        }

        public bool deletarCliente(Cliente cliente) 
        {
            Boolean result;
            try 
            {
                IQuery<Cliente> consult = clienteDb;
                result = consult.delete(cliente);
                return result;
            }
            catch(Exception ex)
            {
                return false;
            }
        }

        public List<Cliente> ListarTodosClientes() 
        {
            try
            {
                IQuery<Cliente> consult = clienteDb;
                return consult.getAllList();
            }catch(Exception ex)
            {
                return null;
            }
        }

        public List<Cliente> ListarClientesPorId(int id)
        {
            try
            {
                IQuery<Cliente> consult = clienteDb;
                return consult.getListById(id);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public bool atualizarCliente(Cliente cliente)
        {
            Boolean result;
            try
            {
                IQuery<Cliente> consult = clienteDb;
                result = consult.update(cliente);
                return result;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool comprarProduto(Compra compra) 
        {
            Boolean result;
            try 
            {
                compraDb.openConnection();
                IQuery<Compra> consult = compraDb;
                result = consult.save(compra);
                compraDb.closeConnection();
                return result;
            }catch(SqlException sqlex)
            {
                return false;
            }

        }


    }
}
